/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <pluginHoster.h>

PluginHoster::PluginHoster(QString const& lib)
{
    _loaded = false;

    QFile _pluginFile(lib);

    if(!_pluginFile.exists()) throw(EXCEPTION_PLUGIN_HOSTER_FILE_NOT_FOUND());

    try
    {
        pluginLoader = new QPluginLoader(_pluginFile.fileName());

        //todo: bei fehlerhafter dll wird try catch ignoriert -> bus error
        //if(pluginLoader->errorString() == "Unknown error") throw(EXCEPTION_PLUGIN_HOSTER_PLUGIN()); //geht nicht, ist immer Unknown error

        hosterObject = pluginLoader->instance();
    } catch ( ... )
    {
        throw(EXCEPTION_PLUGIN_HOSTER_PLUGIN());
    }

    if(hosterObject)
    {
        pluginFile = _pluginFile.fileName();

        hosterInterface = qobject_cast<InterfaceHoster *>(hosterObject);

        if(hosterInterface)
        {
            _info = hosterInterface->info();
        } else
        {
            delete hosterObject;

            throw(EXCEPTION_PLUGIN_HOSTER_INTERFACE());
        }
    } else
    {
        //qDebug() << pluginLoader->errorString();
        throw(EXCEPTION_PLUGIN_HOSTER_OBJECT());
    }

    _loaded = true;
}

PluginHoster::~PluginHoster()
{
    delete hosterInterface;
    delete hosterObject;

    if(pluginLoader->isLoaded()) pluginLoader->unload();

    pluginLoader->deleteLater();
}

QPair<InterfaceHoster*, QObject* > PluginHoster::createHosterObj()
{
    if(hosterInterface)
    {
        QObject *tmpObject = hosterInterface->clone();

        if(tmpObject)
        {
            return qMakePair(qobject_cast<InterfaceHoster *>(tmpObject), tmpObject);
        } else
        {
            throw(EXCEPTION_PLUGIN_HOSTER_CLONE());
        }
    } else
    {
        throw(EXCEPTION_PLUGIN_HOSTER_NOT_LAODED());
    }
}

bool PluginHoster::isValidHoster(QString const& host)
{
    if(host.indexOf(this->_info.reHost) == -1)
        return false;
    else
        return true;
}

bool PluginHoster::canStartDl(InterfaceHoster::E_MODE const& mode)
{
    //qDebug() << QString("free dl: %1, max: %2 | premium dl: %3, max: %4 | MAX: %5").arg(_info.free.downloads).arg(_info.free.maxDownloads).arg(_info.premium.downloads).arg(_info.premium.maxDownloads).arg(_info.maxDownloads);

    if(mode == InterfaceHoster::MODE_FREE)
    {
        if(_info.free.maxDownloads > 0 && _info.free.downloads >= _info.free.maxDownloads) return false;
        if(_info.maxDownloads > 0 && (_info.free.downloads + _info.premium.downloads) >= _info.maxDownloads) return false;
    } else
    {
        if(_info.premium.maxDownloads > 0 && _info.premium.downloads >= _info.premium.maxDownloads) return false;
        if(_info.maxDownloads > 0 && (_info.free.downloads + _info.premium.downloads) >= _info.maxDownloads) return false;
    }

    return true;
}

void PluginHoster::incDownloads(InterfaceHoster::E_MODE const& mode)
{
    if(mode == InterfaceHoster::MODE_FREE)
        ++_info.free.downloads;
    else
        ++_info.premium.downloads;
}

void PluginHoster::decDownloads(InterfaceHoster::E_MODE const& mode)
{
    if(mode == InterfaceHoster::MODE_FREE)
        --_info.free.downloads;
    else
        --_info.premium.downloads;
}
